﻿#region  Copyright (C) 2013 Unisys Corporation

// =============================================================================================
// All Rights Reserved. This work contains trade secrets and confidential material of UNISYS, 
// and its use or disclosure in whole or in part without explicit written permission of 
// UNISYS is prohibited. 
//  
// Product Name          :  Unisys e-Workflow and Imaging 8.2
// File Name             :  DuplicateRules.cs
// Authored By           :  Unisys Corporation
// =============================================================================================
//	Modified by                         Date            Version
// =============================================================================================
//	ronald.aguilar@cr.unisys.com      Jun 06, 2013    Initial Version
// =============================================================================================

#endregion
#region Using Region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using Unisys.DuplicateCheck.Entity.Helper;
using System.Configuration;
#endregion

namespace Unisys.DuplicateCheck.Entity.Model
{
    [Serializable]
    [XmlRoot(ElementName = "DuplicateService", IsNullable = false)]
    public class DuplicateService
    {
        public List<IndexField> getFieldsClass(string Class)
        {

            foreach (DocClass docClass in DocClassList)
            {
                if(docClass.Classes.Equals(Class))
                return docClass.Fields;

            }
            return null;
        }

        [XmlElement("DocClasses")]
        public List<DocClass> DocClassList { get; set; }


        
        [XmlElement("DocClassExc")]
        public List<DocClassExc> DocClassExcList { get; set; }

        [XmlElement("Incosistencies")]
        public List<Incosistence> IncosistenciesList { get; set; }

        [XmlElement("Rejected")]
        public List<Rejected> RejectedClass { get; set; }

        
        #region Methods

        public static DuplicateService loadBusinessRules()
        {
            DuplicateService duplicate = new DuplicateService();
            duplicate = Deserializer.Deserialize<DuplicateService>(Configuration.duplicateRules);
            return duplicate;

        }

        #region GetData
        public Incosistence getIncosistence(string className)
        {
            foreach (Incosistence incon in IncosistenciesList)
            {
                if (incon.Name.Equals(className))
                    return incon;
            }
            return null;
        }

        public DocClassExc getException(string className, string unitName)
        {
            foreach (DocClassExc exc in DocClassExcList)
            {
                if (exc.ClassName.Equals(className))
                    if (exc.UnitName.Equals(unitName))
                        return exc;
            }
            return null;
        }

        public DocClass getDuplicateClass(string className)
        {
            foreach (DocClass docClass in DocClassList)
            {
                if (docClass.Classes.Equals(className))
                    return docClass;
            }
            return null;
        }

        public Rejected getRejectedClass(string className)
        {
            foreach (Rejected rejected in RejectedClass)
            {
                if (rejected.Name.Equals(className))
                    return rejected;
            }
            return null;
        }

        #endregion

        #region Strings for Queries


        public string getDuplicateClasses(DocClass docClass)
        {
            string result = "";
            List<string> list = new List<string>();
        
                foreach (DocClasses docClasses in docClass.Classes)
                {
                    list.Add(docClasses.Class);
                }

                foreach (DocClassExc DocClassExcElement in DocClassExcList)
                {
                    list.Add(DocClassExcElement.ClassName);
                }

                foreach (Incosistence IncosistenceElement in IncosistenciesList)
                {
                    list.Add(IncosistenceElement.Name);

                }

                foreach (Rejected reject in RejectedClass)
                {
                    list.Add(reject.Name);
                }
                list=list.Distinct().ToList();

                foreach (string c in list)
                {
                    result += " '" + c + "',";
                }

                
            result = result.Remove(result.Length - 1);
            return result;
        }


        /// <summary>
        /// Return all index fields corresponding the class configurated in the bussiness rules, discarding duplicates fields.
        /// </summary>
        /// <returns> Return an object like string</returns>
        public List<string> getAllFields( DocClass clase)
        {
            List<string> list = new List<string>();
            
                foreach (IndexField field in clase.Fields)
                {
                    list.Add(field.Name);
                }
            

            list.Distinct().ToList();
            return list;
        }
        #endregion

        #endregion

    }

    [Serializable]
    public class DocClass
    {
        
        [XmlElement("Class")]
        public List<DocClasses> Classes { get; set; } 


        [XmlElement("Field")]
        public List<IndexField> Fields { get; set; }

    }


    [Serializable]
    public class DocClassExc
    {
        [XmlAttribute("ClassName")]
        public string ClassName { get; set; }

        [XmlAttribute("UnitName")]
        public string UnitName { get; set; }

    }

    [Serializable]
    public class DocClasses
    {
        [XmlAttribute("Name")]
        public string Class { get; set; }
    }

    [Serializable]
    public class Incosistence
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlElement("Field")]
        public List<IndexField> Fields { get; set; }

    }

    public class duplicateDocument
    {
        public List<IndexField> Fields { get; set; }

        public string getVENCEValue()
        {
            foreach (IndexField field in Fields)
            {
                if (field.Name.Equals("VENCE"))//encuentra el campo vence y devuelve su valor
                    return field.Name;
            }
            return "";
        }
        public string getIDValue()
        {
            foreach (IndexField field in Fields)
            {
                if ((field.Name.Equals("ID")) || (field.Name.Equals("OBJECT_ID")))//encuentra el campo ID y devuelve su valor
                    return field.Value;
            }
            return "";
        }

        public string getValue(string indexName)
        {
            foreach (IndexField field in Fields)
            {
                if (field.Name.Equals(indexName))//encuentra el campo y devuelve su valor
                    return field.Value;
            }
            return "";
        }

    }

    [Serializable]
    public class IndexField
    {
        
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlText]
        public string Value { get; set; }
    }

    [Serializable]
    public class Rejected
    {

        [XmlAttribute("Name")]
        public string Name { get; set; }


        [XmlAttribute("FieldName")]
        public string FieldName { get; set; }

        [XmlAttribute("FieldValue")]
        public string FieldValue { get; set; }



    }
}
